package day03;

import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/10
 */
public class Pi {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入pi计算项数：");
        int num = sc.nextInt();
        double pi = 0.0;

        for (int i = 1; i <= num; i++) {
            double currentTerm = 1.0 / (2 * i - 1); // 计算当前项的值
            if (i % 2 != 0) { // 奇数项为正
                pi += currentTerm;
            } else { // 偶数项为负
                pi -= currentTerm;
            }
        }

        pi *= 4;

        System.out.println("pi近似值: " + pi);
    }
}
